<html>
<head>
<title>ExpenseTracker.java</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
.s0 { color: #cf8e6d;}
.s1 { color: #bcbec4;}
.s2 { color: #bcbec4;}
.s3 { color: #7a7e85;}
.s4 { color: #2aacb8;}
.s5 { color: #6aab73;}
</style>
</head>
<body bgcolor="#1e1f22">
<table CELLSPACING=0 CELLPADDING=5 COLS=1 WIDTH="100%" BGCOLOR="#606060" >
<tr><td><center>
<font face="Arial, Helvetica" color="#000000">
ExpenseTracker.java</font>
</center></td></tr></table>
<pre><span class="s0">package </span><span class="s1">git_big_work</span><span class="s2">;</span>

<span class="s0">import </span><span class="s1">java</span><span class="s2">.</span><span class="s1">util</span><span class="s2">.</span><span class="s1">ArrayList</span><span class="s2">;</span>
<span class="s0">import </span><span class="s1">java</span><span class="s2">.</span><span class="s1">util</span><span class="s2">.</span><span class="s1">List</span><span class="s2">;</span>
<span class="s0">import </span><span class="s1">java</span><span class="s2">.</span><span class="s1">util</span><span class="s2">.</span><span class="s1">Scanner</span><span class="s2">;</span>

<span class="s0">public class </span><span class="s1">ExpenseTracker </span><span class="s2">{</span>
    <span class="s0">public static void </span><span class="s1">main</span><span class="s2">(</span><span class="s1">String</span><span class="s2">[] </span><span class="s1">args</span><span class="s2">) {</span>
        <span class="s3">// 创建用于存储收入和支出的列表</span>
        <span class="s1">List</span><span class="s2">&lt;</span><span class="s1">Double</span><span class="s2">&gt; </span><span class="s1">income </span><span class="s2">= </span><span class="s0">new </span><span class="s1">ArrayList</span><span class="s2">&lt;&gt;();</span>
        <span class="s1">List</span><span class="s2">&lt;</span><span class="s1">Double</span><span class="s2">&gt; </span><span class="s1">expenses </span><span class="s2">= </span><span class="s0">new </span><span class="s1">ArrayList</span><span class="s2">&lt;&gt;();</span>

        <span class="s3">// 设置初始预算</span>
        <span class="s0">double </span><span class="s1">budget </span><span class="s2">= </span><span class="s4">1000.0</span><span class="s2">;</span>

        <span class="s3">// 创建 Scanner 对象用于读取用户输入</span>
        <span class="s1">Scanner scanner </span><span class="s2">= </span><span class="s0">new </span><span class="s1">Scanner</span><span class="s2">(</span><span class="s1">System</span><span class="s2">.</span><span class="s1">in</span><span class="s2">);</span>

        <span class="s3">// 控制程序运行的标志</span>
        <span class="s0">boolean </span><span class="s1">running </span><span class="s2">= </span><span class="s0">true</span><span class="s2">;</span>

        <span class="s3">// 主循环，根据用户选择执行不同的操作</span>
        <span class="s0">while </span><span class="s2">(</span><span class="s1">running</span><span class="s2">) {</span>
            <span class="s1">System</span><span class="s2">.</span><span class="s1">out</span><span class="s2">.</span><span class="s1">println</span><span class="s2">(</span><span class="s5">&quot;请选择操作：&quot;</span><span class="s2">);</span>
            <span class="s1">System</span><span class="s2">.</span><span class="s1">out</span><span class="s2">.</span><span class="s1">println</span><span class="s2">(</span><span class="s5">&quot;1. 记录收入&quot;</span><span class="s2">);</span>
            <span class="s1">System</span><span class="s2">.</span><span class="s1">out</span><span class="s2">.</span><span class="s1">println</span><span class="s2">(</span><span class="s5">&quot;2. 记录支出&quot;</span><span class="s2">);</span>
            <span class="s1">System</span><span class="s2">.</span><span class="s1">out</span><span class="s2">.</span><span class="s1">println</span><span class="s2">(</span><span class="s5">&quot;3. 显示总收入和总支出&quot;</span><span class="s2">);</span>
            <span class="s1">System</span><span class="s2">.</span><span class="s1">out</span><span class="s2">.</span><span class="s1">println</span><span class="s2">(</span><span class="s5">&quot;4. 查看所有账单&quot;</span><span class="s2">);</span>
            <span class="s1">System</span><span class="s2">.</span><span class="s1">out</span><span class="s2">.</span><span class="s1">println</span><span class="s2">(</span><span class="s5">&quot;5. 查询账单&quot;</span><span class="s2">);</span>
            <span class="s1">System</span><span class="s2">.</span><span class="s1">out</span><span class="s2">.</span><span class="s1">println</span><span class="s2">(</span><span class="s5">&quot;6. 设置预算&quot;</span><span class="s2">);</span>
            <span class="s1">System</span><span class="s2">.</span><span class="s1">out</span><span class="s2">.</span><span class="s1">println</span><span class="s2">(</span><span class="s5">&quot;7. 月度统计&quot;</span><span class="s2">);</span>
            <span class="s1">System</span><span class="s2">.</span><span class="s1">out</span><span class="s2">.</span><span class="s1">println</span><span class="s2">(</span><span class="s5">&quot;8. 退出&quot;</span><span class="s2">);</span>
            <span class="s1">System</span><span class="s2">.</span><span class="s1">out</span><span class="s2">.</span><span class="s1">print</span><span class="s2">(</span><span class="s5">&quot;请输入选项（1-8）：&quot;</span><span class="s2">);</span>

            <span class="s3">// 读取用户输入的选择</span>
            <span class="s0">int </span><span class="s1">choice </span><span class="s2">= </span><span class="s1">scanner</span><span class="s2">.</span><span class="s1">nextInt</span><span class="s2">();</span>
            <span class="s1">scanner</span><span class="s2">.</span><span class="s1">nextLine</span><span class="s2">(); </span><span class="s3">// 消耗掉换行符，以便下一次读取</span>

            <span class="s3">// 根据用户选择执行相应的操作</span>
            <span class="s0">switch </span><span class="s2">(</span><span class="s1">choice</span><span class="s2">) {</span>
                <span class="s0">case </span><span class="s4">1</span><span class="s2">:</span>
                    <span class="s1">System</span><span class="s2">.</span><span class="s1">out</span><span class="s2">.</span><span class="s1">print</span><span class="s2">(</span><span class="s5">&quot;请输入收入金额：&quot;</span><span class="s2">);</span>
                    <span class="s0">double </span><span class="s1">incomeAmount </span><span class="s2">= </span><span class="s1">scanner</span><span class="s2">.</span><span class="s1">nextDouble</span><span class="s2">();</span>
                    <span class="s1">income</span><span class="s2">.</span><span class="s1">add</span><span class="s2">(</span><span class="s1">incomeAmount</span><span class="s2">);</span>
                    <span class="s0">break</span><span class="s2">;</span>
                <span class="s0">case </span><span class="s4">2</span><span class="s2">:</span>
                    <span class="s1">System</span><span class="s2">.</span><span class="s1">out</span><span class="s2">.</span><span class="s1">print</span><span class="s2">(</span><span class="s5">&quot;请输入支出金额：&quot;</span><span class="s2">);</span>
                    <span class="s0">double </span><span class="s1">expenseAmount </span><span class="s2">= </span><span class="s1">scanner</span><span class="s2">.</span><span class="s1">nextDouble</span><span class="s2">();</span>
                    <span class="s1">expenses</span><span class="s2">.</span><span class="s1">add</span><span class="s2">(</span><span class="s1">expenseAmount</span><span class="s2">);</span>
                    <span class="s0">break</span><span class="s2">;</span>
                <span class="s0">case </span><span class="s4">3</span><span class="s2">:</span>
                    <span class="s3">// 调用方法计算总收入和总支出</span>
                    <span class="s0">double </span><span class="s1">totalIncome </span><span class="s2">= </span><span class="s1">sumList</span><span class="s2">(</span><span class="s1">income</span><span class="s2">);</span>
                    <span class="s0">double </span><span class="s1">totalExpenses </span><span class="s2">= </span><span class="s1">sumList</span><span class="s2">(</span><span class="s1">expenses</span><span class="s2">);</span>
                    <span class="s1">System</span><span class="s2">.</span><span class="s1">out</span><span class="s2">.</span><span class="s1">println</span><span class="s2">(</span><span class="s5">&quot;总收入：&quot; </span><span class="s2">+ </span><span class="s1">totalIncome</span><span class="s2">);</span>
                    <span class="s1">System</span><span class="s2">.</span><span class="s1">out</span><span class="s2">.</span><span class="s1">println</span><span class="s2">(</span><span class="s5">&quot;总支出：&quot; </span><span class="s2">+ </span><span class="s1">totalExpenses</span><span class="s2">);</span>
                    <span class="s0">break</span><span class="s2">;</span>
                <span class="s0">case </span><span class="s4">4</span><span class="s2">:</span>
                    <span class="s1">System</span><span class="s2">.</span><span class="s1">out</span><span class="s2">.</span><span class="s1">println</span><span class="s2">(</span><span class="s5">&quot;所有收入账单：&quot;</span><span class="s2">);</span>
                    <span class="s0">for </span><span class="s2">(</span><span class="s1">Double in </span><span class="s2">: </span><span class="s1">income</span><span class="s2">) {</span>
                        <span class="s1">System</span><span class="s2">.</span><span class="s1">out</span><span class="s2">.</span><span class="s1">println</span><span class="s2">(</span><span class="s1">in</span><span class="s2">);</span>
                    <span class="s2">}</span>
                    <span class="s1">System</span><span class="s2">.</span><span class="s1">out</span><span class="s2">.</span><span class="s1">println</span><span class="s2">(</span><span class="s5">&quot;所有支出账单：&quot;</span><span class="s2">);</span>
                    <span class="s0">for </span><span class="s2">(</span><span class="s1">Double ex </span><span class="s2">: </span><span class="s1">expenses</span><span class="s2">) {</span>
                        <span class="s1">System</span><span class="s2">.</span><span class="s1">out</span><span class="s2">.</span><span class="s1">println</span><span class="s2">(</span><span class="s1">ex</span><span class="s2">);</span>
                    <span class="s2">}</span>
                    <span class="s0">break</span><span class="s2">;</span>
                <span class="s0">case </span><span class="s4">5</span><span class="s2">:</span>
                    <span class="s1">System</span><span class="s2">.</span><span class="s1">out</span><span class="s2">.</span><span class="s1">println</span><span class="s2">(</span><span class="s5">&quot;请选择查询类型（1.收入 2.支出）：&quot;</span><span class="s2">);</span>
                    <span class="s0">int </span><span class="s1">queryChoice </span><span class="s2">= </span><span class="s1">scanner</span><span class="s2">.</span><span class="s1">nextInt</span><span class="s2">();</span>
                    <span class="s1">scanner</span><span class="s2">.</span><span class="s1">nextLine</span><span class="s2">();</span>
                    <span class="s0">if </span><span class="s2">(</span><span class="s1">queryChoice </span><span class="s2">== </span><span class="s4">1</span><span class="s2">) {</span>
                        <span class="s1">System</span><span class="s2">.</span><span class="s1">out</span><span class="s2">.</span><span class="s1">println</span><span class="s2">(</span><span class="s5">&quot;请输入要查询的收入金额：&quot;</span><span class="s2">);</span>
                        <span class="s0">double </span><span class="s1">queryIncomeAmount </span><span class="s2">= </span><span class="s1">scanner</span><span class="s2">.</span><span class="s1">nextDouble</span><span class="s2">();</span>
                        <span class="s0">boolean </span><span class="s1">foundIncome </span><span class="s2">= </span><span class="s0">false</span><span class="s2">;</span>
                        <span class="s0">for </span><span class="s2">(</span><span class="s0">int </span><span class="s1">i </span><span class="s2">= </span><span class="s4">0</span><span class="s2">; </span><span class="s1">i </span><span class="s2">&lt; </span><span class="s1">income</span><span class="s2">.</span><span class="s1">size</span><span class="s2">(); </span><span class="s1">i</span><span class="s2">++) {</span>
                            <span class="s0">if </span><span class="s2">(</span><span class="s1">income</span><span class="s2">.</span><span class="s1">get</span><span class="s2">(</span><span class="s1">i</span><span class="s2">).</span><span class="s1">equals</span><span class="s2">(</span><span class="s1">queryIncomeAmount</span><span class="s2">)) {</span>
                                <span class="s1">System</span><span class="s2">.</span><span class="s1">out</span><span class="s2">.</span><span class="s1">println</span><span class="s2">(</span><span class="s5">&quot;找到收入账单，金额为：&quot; </span><span class="s2">+ </span><span class="s1">queryIncomeAmount </span><span class="s2">+ </span><span class="s5">&quot;，在列表中的位置为：&quot; </span><span class="s2">+ </span><span class="s1">i</span><span class="s2">);</span>
                                <span class="s1">foundIncome </span><span class="s2">= </span><span class="s0">true</span><span class="s2">;</span>
                                <span class="s0">break</span><span class="s2">;</span>
                            <span class="s2">}</span>
                        <span class="s2">}</span>
                        <span class="s0">if </span><span class="s2">(!</span><span class="s1">foundIncome</span><span class="s2">) {</span>
                            <span class="s1">System</span><span class="s2">.</span><span class="s1">out</span><span class="s2">.</span><span class="s1">println</span><span class="s2">(</span><span class="s5">&quot;未找到该收入账单。&quot;</span><span class="s2">);</span>
                        <span class="s2">}</span>
                    <span class="s2">} </span><span class="s0">else if </span><span class="s2">(</span><span class="s1">queryChoice </span><span class="s2">== </span><span class="s4">2</span><span class="s2">) {</span>
                        <span class="s1">System</span><span class="s2">.</span><span class="s1">out</span><span class="s2">.</span><span class="s1">println</span><span class="s2">(</span><span class="s5">&quot;请输入要查询的支出金额：&quot;</span><span class="s2">);</span>
                        <span class="s0">double </span><span class="s1">queryExpenseAmount </span><span class="s2">= </span><span class="s1">scanner</span><span class="s2">.</span><span class="s1">nextDouble</span><span class="s2">();</span>
                        <span class="s0">boolean </span><span class="s1">foundExpense </span><span class="s2">= </span><span class="s0">false</span><span class="s2">;</span>
                        <span class="s0">for </span><span class="s2">(</span><span class="s0">int </span><span class="s1">i </span><span class="s2">= </span><span class="s4">0</span><span class="s2">; </span><span class="s1">i </span><span class="s2">&lt; </span><span class="s1">expenses</span><span class="s2">.</span><span class="s1">size</span><span class="s2">(); </span><span class="s1">i</span><span class="s2">++) {</span>
                            <span class="s0">if </span><span class="s2">(</span><span class="s1">expenses</span><span class="s2">.</span><span class="s1">get</span><span class="s2">(</span><span class="s1">i</span><span class="s2">).</span><span class="s1">equals</span><span class="s2">(</span><span class="s1">queryExpenseAmount</span><span class="s2">)) {</span>
                                <span class="s1">System</span><span class="s2">.</span><span class="s1">out</span><span class="s2">.</span><span class="s1">println</span><span class="s2">(</span><span class="s5">&quot;找到支出账单，金额为：&quot; </span><span class="s2">+ </span><span class="s1">queryExpenseAmount </span><span class="s2">+ </span><span class="s5">&quot;，在列表中的位置为：&quot; </span><span class="s2">+ </span><span class="s1">i</span><span class="s2">);</span>
                                <span class="s1">foundExpense </span><span class="s2">= </span><span class="s0">true</span><span class="s2">;</span>
                                <span class="s0">break</span><span class="s2">;</span>
                            <span class="s2">}</span>
                        <span class="s2">}</span>
                        <span class="s0">if </span><span class="s2">(!</span><span class="s1">foundExpense</span><span class="s2">) {</span>
                            <span class="s1">System</span><span class="s2">.</span><span class="s1">out</span><span class="s2">.</span><span class="s1">println</span><span class="s2">(</span><span class="s5">&quot;未找到该支出账单。&quot;</span><span class="s2">);</span>
                        <span class="s2">}</span>
                    <span class="s2">} </span><span class="s0">else </span><span class="s2">{</span>
                        <span class="s1">System</span><span class="s2">.</span><span class="s1">out</span><span class="s2">.</span><span class="s1">println</span><span class="s2">(</span><span class="s5">&quot;无效的查询类型选项。&quot;</span><span class="s2">);</span>
                    <span class="s2">}</span>
                    <span class="s0">break</span><span class="s2">;</span>
                <span class="s0">case </span><span class="s4">6</span><span class="s2">:</span>
                    <span class="s1">System</span><span class="s2">.</span><span class="s1">out</span><span class="s2">.</span><span class="s1">print</span><span class="s2">(</span><span class="s5">&quot;请输入新的预算金额：&quot;</span><span class="s2">);</span>
                    <span class="s1">budget </span><span class="s2">= </span><span class="s1">scanner</span><span class="s2">.</span><span class="s1">nextDouble</span><span class="s2">();</span>
                    <span class="s0">break</span><span class="s2">;</span>
                <span class="s0">case </span><span class="s4">7</span><span class="s2">:</span>
                    <span class="s0">double </span><span class="s1">totalIncomeForMonth </span><span class="s2">= </span><span class="s1">sumList</span><span class="s2">(</span><span class="s1">income</span><span class="s2">);</span>
                    <span class="s0">double </span><span class="s1">totalExpensesForMonth </span><span class="s2">= </span><span class="s1">sumList</span><span class="s2">(</span><span class="s1">expenses</span><span class="s2">);</span>
                    <span class="s0">double </span><span class="s1">remainingBudget </span><span class="s2">= </span><span class="s1">budget </span><span class="s2">- </span><span class="s1">totalExpensesForMonth</span><span class="s2">;</span>
                    <span class="s1">System</span><span class="s2">.</span><span class="s1">out</span><span class="s2">.</span><span class="s1">println</span><span class="s2">(</span><span class="s5">&quot;本月总收入：&quot; </span><span class="s2">+ </span><span class="s1">totalIncomeForMonth</span><span class="s2">);</span>
                    <span class="s1">System</span><span class="s2">.</span><span class="s1">out</span><span class="s2">.</span><span class="s1">println</span><span class="s2">(</span><span class="s5">&quot;本月总支出：&quot; </span><span class="s2">+ </span><span class="s1">totalExpensesForMonth</span><span class="s2">);</span>
                    <span class="s1">System</span><span class="s2">.</span><span class="s1">out</span><span class="s2">.</span><span class="s1">println</span><span class="s2">(</span><span class="s5">&quot;预算余额：&quot; </span><span class="s2">+ </span><span class="s1">remainingBudget</span><span class="s2">);</span>
                    <span class="s0">break</span><span class="s2">;</span>
                <span class="s0">case </span><span class="s4">8</span><span class="s2">:</span>
                    <span class="s1">running </span><span class="s2">= </span><span class="s0">false</span><span class="s2">;</span>
                    <span class="s1">System</span><span class="s2">.</span><span class="s1">out</span><span class="s2">.</span><span class="s1">println</span><span class="s2">(</span><span class="s5">&quot;程序已退出。&quot;</span><span class="s2">);</span>
                    <span class="s0">break</span><span class="s2">;</span>
                <span class="s0">default</span><span class="s2">:</span>
                    <span class="s1">System</span><span class="s2">.</span><span class="s1">out</span><span class="s2">.</span><span class="s1">println</span><span class="s2">(</span><span class="s5">&quot;无效的选项，请重新输入。&quot;</span><span class="s2">);</span>
            <span class="s2">}</span>
        <span class="s2">}</span>

        <span class="s3">// 关闭 Scanner 对象</span>
        <span class="s1">scanner</span><span class="s2">.</span><span class="s1">close</span><span class="s2">();</span>
    <span class="s2">}</span>

    <span class="s3">// 计算列表中所有数字的总和</span>
    <span class="s0">private static double </span><span class="s1">sumList</span><span class="s2">(</span><span class="s1">List</span><span class="s2">&lt;</span><span class="s1">Double</span><span class="s2">&gt; </span><span class="s1">list</span><span class="s2">) {</span>
        <span class="s0">double </span><span class="s1">sum </span><span class="s2">= </span><span class="s4">0</span><span class="s2">;</span>
        <span class="s0">for </span><span class="s2">(</span><span class="s0">double </span><span class="s1">number </span><span class="s2">: </span><span class="s1">list</span><span class="s2">) {</span>
            <span class="s1">sum </span><span class="s2">+= </span><span class="s1">number</span><span class="s2">;</span>
        <span class="s2">}</span>
        <span class="s0">return </span><span class="s1">sum</span><span class="s2">;</span>
    <span class="s2">}</span>
<span class="s2">}</span></pre>
</body>
</html>